package com.woniuxy.configuration;

import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.woniuxy.realm.UserRealm;


@Configuration   //将当前类指定为配置类 等价于shiro的xml
public class ShiroConfiguration {
	//配置realm
	@Bean
	public UserRealm getReam(){
		UserRealm realm = new UserRealm();
		return realm;
	}
	//安全管理器
	@Bean
	public DefaultWebSecurityManager getManager(UserRealm realm){
		DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
		manager.setRealm(realm);
		return manager;
	}
	//过滤器
	@Bean
	public ShiroFilterFactoryBean getFactoryBean(DefaultWebSecurityManager manager){
		ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
		bean.setSecurityManager(manager);
		//登录页面
		bean.setLoginUrl("/index.html");
		//没有权限
		bean.setUnauthorizedUrl("/html/error.html");
		//过滤器链
		Map<String, String> map = new LinkedHashMap<>(); //需要保证有序
		
		map.put("/index.html", "anon");
		map.put("/user/login", "anon");
		
		map.put("/druid/**", "anon");
		map.put("/**", "anon");
		
		bean.setFilterChainDefinitionMap(map);
		return bean;
	}
}
